package com.javatest.springboot2basictest.service;

import com.javatest.springboot2basictest.response.ResponseMessage;
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import java.nio.charset.StandardCharsets;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2020-07-03 上午9:11
 */
@Service
public class RestClientService {
    public static void main(String[] args) {
        final HttpClient httpClient = HttpClient.create().tcpConfiguration(client -> {
            client.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10_000);
            client.doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler(10)));
            return client;
        });

        ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);

        WebClient webClient = WebClient.builder()
                .clientConnector(connector)
                .baseUrl("http://localhost:8014/app/userLogManagement")
                .defaultHeader("Authorization", "login_token_a1179471-a9db-4b4d-b353-788a60968ab3")
                .build();

        Mono<ResponseMessage> response = webClient.get()
                .uri("/getPagedUserLog?pageNo=16&pageSize=20")
                .accept(MediaType.APPLICATION_JSON).acceptCharset(StandardCharsets.UTF_8)
                .retrieve()
                .bodyToMono(ResponseMessage.class);

        final ResponseMessage message = response.block();
        System.out.println(message);
    }

}
